package controller;

import network.Message;
import network.MessageDispatcher;
import network.client.WhiteboardClient;
import network.server.WhiteboardServer;

/**
 * A network controller provides a common operation over the network. Also, it
 * keeps the reference of server and client and other useful object.
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class NetworkController {
	private WhiteboardClient client = null;
	private WhiteboardServer server = null;
	private String username = null;

	private static NetworkController instance = null;

	private NetworkController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * @return an instance of the network controller
	 */
	public static NetworkController getInstance() {
		if (instance == null) {
			instance = new NetworkController();

		}
		return instance;
	}

	/**
	 * Send a message to the Server.
	 * @param msg
	 */
	public void sendMessage(Message msg) {
		try {
			MessageDispatcher.dispatch(client.getClientSocket(), msg);
		} catch (Exception e) {
			if (Constants.DEBUG) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @return whiteboard client reference 
	 */
	public WhiteboardClient getClient() {
		return client;
	}

	public void setClient(WhiteboardClient client) {
		this.client = client;
	}

	public WhiteboardServer getServer() {
		return server;
	}

	public void setServer(WhiteboardServer server) {
		this.server = server;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
